package bigo.todolist.avocado;

import android.content.ContentValues;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * A java class representing a database that holds the categories of the task list
 * @authorAlex Irlik, Steve Fox, TiOluwa Olarewaju, Derek Miller
 * Big-Ovacado Task Force
 * Version 1.0
 *
 */
public class DBCategoryAdapter {

	public static final String KEY_ROWID = "_id";
    public static final String KEY_CATEGORY = "categoryName";
    private static final String TAG = "DBAdapter";
    
    private static final String DATABASE_NAME = "categorydb";
    private static final String DATABASE_TABLE_CATEGORY = "category";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table category (_id integer primary key autoincrement, "
    	+ "categoryName text not null);";
        
    private Context context = null;  
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    /**
     * Database Adaptor constructor
     * @param ctx
     */
    public DBCategoryAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    
    /** 
     * Database helper class
     * @author Big Ohvocodo Task Force
     */  
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS category");
            onCreate(db);
        }
    }    
    
    /**
     * Open database
     * @throws SQLException
     */
    public void open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
    }
    
    /**
     * Close database file
     */
      
    public void close() 
    {
        DBHelper.close();
    }    
    
    /**
     * Add Categoriy name to the database
     * @param taskName
     * @param date
     * @param description
     * @return values
     */
    
    public long AddCategory(String category)
    {
    	ContentValues initialValues = new ContentValues();
    	initialValues.put(KEY_CATEGORY, category);
    	return db.insert(DATABASE_TABLE_CATEGORY, null, initialValues);
    }
    

    /**
     * Retrieve a categories from the database
     * @param page
     * @return
     */
    public String[] getCategory(boolean page)
    {
    	ArrayList<String> catGrab = new ArrayList<String>();
    	if(page == true) //Determines if all category selections are necessary
    	{
	    	catGrab.add("All");
	    	catGrab.add("Personal");
	    	catGrab.add("Work");
	    	catGrab.add("School");
	    	catGrab.add("Completed");
	    	catGrab.add("Uncompleted");
    	}
    	else //If all categories are unnecessary
    	{
    		catGrab.add("Personal");
	    	catGrab.add("Work");
	    	catGrab.add("School");
    	}
    	//For further implementation when categories can be created
    	/*
    	if(c.getCount() > 0)
    	{
	    	for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
	    	{
	    		if(c.getString(iCategory) != null)
	    			catGrab.add(c.getString(iCategory));
	    	}
    	}
    	*/
    	catGrab.trimToSize(); //trim array list
    	String[] result = new String[catGrab.size()]; //create an array of array list size
    	result = catGrab.toArray(result); //turn array list into array
    	return result;
    }
}
